package com.compass.doctor.ui.home.viewmodel

import androidx.lifecycle.MutableLiveData
import com.compass.doctor.ui.patient.viewmodel.ExamineViewModel
import com.compass.framework.ext.stringListToJson
import com.compass.framework.model.InquiryOrder
import com.compass.framework.model.DoctorUrgency
import com.compass.framework.model.InquiryMessage
import com.compass.framework.model.UnderwayOrder
import com.compass.framework.toast.TipsToast
import com.compass.network.callback.IApiErrorCallback
import com.compass.network.manager.ApiManager
import com.compass.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject

/**
 * Description： 问诊viewModel
 * Date:2023/12/25 15:27
 */
class InquiryOrderViewModel : BaseViewModel() {

    val orderLiveData = MutableLiveData<MutableList<InquiryOrder>?>()
    val orderCountLiveData = MutableLiveData<Int?>()
    val receptionLiveData = MutableLiveData<Boolean>()//接诊操作
    val turnDownLiveData = MutableLiveData<Boolean>()//拒诊诊操作
    val finishLiveData = MutableLiveData<Boolean>()//结束问诊
    val giveLiveData = MutableLiveData<Boolean>()//赠送消息
    val wayOrderLiveData = MutableLiveData<UnderwayOrder?>()//进行中的订单

    val callLiveData = MutableLiveData<Boolean>()//问诊外呼
    val inquiryMessageLiveData = MutableLiveData<InquiryMessage?>()//问诊单



    //1-待接诊 2-已接诊 3-已完成 可不传所有 type
    fun getConsultationOrder(count: Int, pageSize: Int = 10, type: Int) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getConsultationOrder(count, pageSize, type)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                orderLiveData.value = null
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                orderLiveData.value = null
            } else {
                orderLiveData.value = it.list
            }
        })
    }

    //获取正在进行中的问诊订单
    fun getUnderwayOrder(patientId: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getUnderwayOrder(patientId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                wayOrderLiveData.value=null
            }
        }, successBlock = {
            wayOrderLiveData.value=it
        })
    }

    //问诊外呼
    fun consultationCall(id: String,   doctorMobile: String?, mobileType: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.consultationCall(id,doctorMobile,mobileType)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            it?.let {
                callLiveData.value=true
            }

        })
    }

    //今日新增问诊数量
    fun getConsultationOrderCount() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.consultationOrderCount()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                orderCountLiveData.value = null
            }
        }, successBlock = {
            orderCountLiveData.value = it
        })
    }


    //获取紧急待办详情
    fun getUrgencyDetails(orderId:String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getInquiryOrderDetails(orderId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            inquiryMessageLiveData.value = it
        })
    }


    //医生接诊 or 拒诊
    fun clinicalReception(orderId :String , status: Int ,rejectMsg :String="") {
        val params = JsonObject().apply {
            addProperty("orderId",orderId)
            addProperty("status",status)
            addProperty("rejectMsg",rejectMsg)//用户类型 3医生
        }

        launchUIWithResult(responseBlock = {
            ApiManager.api.clinicalReception(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                if (status==1){
                    receptionLiveData.value = false
                }else{
                    turnDownLiveData.value=false
                }


            }
        }, successBlock = {
            it?.let {
                if (status==1){
                    receptionLiveData.value = true
                }else{
                    turnDownLiveData.value=true
                }
            }

        })
    }

    //医生结束问诊
    fun finishInterrogation(orderId :String? ,rejectMsg :String?="") {
        val params = JsonObject().apply {
            addProperty("orderId",orderId)
            addProperty("msg",rejectMsg)

        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.finishInterrogation(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            it?.let {
                finishLiveData.value = true
            }
        })
    }

    //医生退诊
    fun retreatOrder(orderId :String? ,rejectMsg :String?="") {
        val params = JsonObject().apply {
            addProperty("orderId",orderId)
            addProperty("rejectMsg",rejectMsg)
//            addProperty("source","3")

        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.retreatOrder(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            it?.let {
                finishLiveData.value = true
            }
        })
    }



    /**
     * 医生向患者赠送条数
     */
    fun giveMessaging(giveNumber:String,giveTime:String,patientId:String) {
        val params = JsonObject().apply {
            addProperty("giveNumber",giveNumber)
            addProperty("giveTime",giveTime)
            addProperty("patientId",patientId)
            addProperty("type","1")//单独发送时1 群发是2
        }

        launchUIWithResult(responseBlock = {
            ApiManager.api.giveMessaging(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                giveLiveData.value=false
            }
        }) {
            it?.let {
                giveLiveData.value=true
            }

        }
    }

}